<?php
namespace Admin\Controller;
use Common\Controller\BackendController;
class SettaocanController extends BackendController {
    public function _initialize() {
        parent::_initialize();
    }
    public function _after_insert($id,$data){
    	$folder = QSCMS_DATA_PATH.'upload/setmeal_img/';
    	if(!file_exists($folder.$id.'.png')){
    		copy($folder.'2.png',$folder.$id.'.png');
    	}
    }

    /**
     * 添加
     * @time 2020/12/15 10:40
     */
    public function add() {
        $mod = D($this->_name);

        $pk = $mod->getPk();
        if(IS_POST){
//            if(false === $data = $mod->create()){
//         //       var_dump($data);
//                IS_AJAX && $this->ajaxReturn(0,$mod->getError());
//                $this->error($mod->getError());
//            }
            $data=$_POST;
            if(method_exists($this,'_before_insert')) {
             //   var_dump($data);
                $data = $this->_before_insert($data);
            }
            $data['vip_val']=serialize($data['vip_val']);
            $data['pt_val']=serialize($data['pt_val']);
            $data['name_val']=serialize($data['name_val']);
          // var_dump($data);die;
            $field = $mod->getDbFields();
            if(C('qscms_subsite_open')==1 && in_array('subsite_id',$field) && D('Subsite')->get_subsite_domain()){
                $subsites = I('request.subsite_id');
                if($subsites == ''){
                    IS_AJAX && $this->ajaxReturn(0, '请选择站点！');
                    $this->error('请选择站点！');
                }
                $subsites = is_array($subsites)? $subsites : array($subsites);
                foreach ($subsites as $val) {
                    $data['subsite_id'] = intval($val);
                    if($id = $mod->add($data)){
                        if(method_exists($this,'_after_insert')){
                            $data[$pk] = $id;
                            $this->_after_insert($id,$data);
                        }

                    }else{
                        $reg = true;
                        break;
                    }
                }
                if(!$reg){
                    IS_AJAX && $this->ajaxReturn(1, L('operation_success'), '', 'add');
                    $this->success(L('operation_success'));
                }else{
                    IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
                    $this->error(L('operation_failure'));
                }
            }else{
                if($id = $mod->add($data)){
                    if(method_exists($this,'_after_insert')){
                        $data[$pk] = $id;
                        $this->_after_insert($id,$data);
                    }
                    IS_AJAX && $this->ajaxReturn(1, L('operation_success'), '', 'add');
                    $this->success(L('operation_success'));
                }else{
                    IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
                    $this->error(L('operation_failure'));
                }
            }
        }else{
            $this->assign('open_validator',true);
            if(IS_AJAX){
                $response = $this->fetch();
                $this->ajaxReturn(1,'',$response);
            }else{
                $this->display($this->_tpl);
            }
        }
    }

    /**
     * 修改
     * @time 2020/12/15 11：40
     */

    public function edit(){
        $mod = D($this->_name);
        $pk = $mod->getPk();
        if (IS_POST) {
//            if (false === $data = $mod->create()) {
//                IS_AJAX && $this->ajaxReturn(0, $mod->getError());
//                $this->error($mod->getError());
//            }
            $data=$_POST;
            if (method_exists($this, '_before_update')) {
                $data = $this->_before_update($data);
            }
            $data['vip_val']=serialize($data['vip_val']);
            $data['pt_val']=serialize($data['pt_val']);
            $data['name_val']=serialize($data['name_val']);
            //   var_dump($data);die;
            if (false !== $mod->save($data)) {
                if( method_exists($this, '_after_update')){
                    $id = $data[$pk];
                    $this->_after_update($id,$data);
                }
                IS_AJAX && $this->ajaxReturn(1, L('operation_success'), '', 'edit');
                $this->success(L('operation_success'));
            } else {
                IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
                $this->error(L('operation_failure'));
            }
        } else {
            $id = I('get.'.$pk,0,'intval');

            $info = $mod->find($id);
            $info['vip_val']=unserialize($info['vip_val']);
            $info['pt_val']=unserialize($info['pt_val']);
            $info['name_val']=unserialize($info['name_val']);


            if( method_exists($this, '_after_select')){
                if($data = $this->_after_select($info)) $info = $data;
            }
            $this->assign('info', $info);
            if (IS_AJAX) {
                $response = $this->fetch();
                $this->ajaxReturn(1, '', $response);
            } else {
                $this->display($this->_tpl);
            }
        }
    }
}